package com.zjw.socket.demo.starter.messageService.service;

import com.zjw.socket.demo.starter.annotation.core.Bean;
import com.zjw.socket.demo.starter.annotation.core.Inject;
import com.zjw.socket.demo.starter.messageService.*;
import com.zjw.socket.demo.starter.messageService.utils.GsonUtils;

import java.nio.charset.StandardCharsets;

@Bean
public class LoginService {

  @Inject(query = "SimpleUserPool")
  public IUserPool userPool;

  @Inject(query = "SimpleUserDataBase")
  public IUserDataBase userDataBase;

  public void login(long userId, Message message, IConnectSession connectSession) {
    User user = userDataBase.queryUserByUsername(userId);
    Message responseMessage = MessageCacheFactory.obtainResponseMessage(message);
    if (user == null) {
      responseMessage.setType(SystemMessageType.LOGIN_NOT_REGISTER);
      connectSession.writeMessage(responseMessage);
      return;
    }

    //解析登录信息，如登录地，登录时间，登录暗文密码
    String json = MessagePayloadUtils.getString(message);
    User loginUser = GsonUtils.GSON.fromJson(json, User.class);
    if (!user.password.equals(loginUser.password)) {
      responseMessage.setType(SystemMessageType.LOGIN_PASSWORD_ERROR);
      connectSession.writeMessage(responseMessage);
      return;
    }

    IConnectSession otherConnect = userPool.getConnectSession(userId);
    if (otherConnect != null && !otherConnect.equals(connectSession)) {
      //repeat login
      responseMessage.setType(SystemMessageType.LOGIN_ANOTHER_DEVICE);
      otherConnect.writeMessage(responseMessage);
    }

    responseMessage.setType(SystemMessageType.LOGIN_SUCCESS);
    connectSession.writeMessage(responseMessage);
    userPool.connect(userId, connectSession);
  }

  public void register(long userId, Message message, IConnectSession connectSession) {
    User user = userDataBase.queryUserByUsername(userId);
    Message responseMessage = MessageCacheFactory.obtainResponseMessage(message);
    if (user != null) {
      responseMessage.setType(SystemMessageType.REGISTER_REPEAT);
      connectSession.writeMessage(responseMessage);
      return;
    }

    //todo 暂定
    user = GsonUtils.GSON.fromJson(MessagePayloadUtils.getString(message), User.class);
    //初始化自身
    user.friends.add(user.toFriend(0));
    responseMessage.setType(SystemMessageType.REGISTER_SUCCESS);
    connectSession.writeMessage(responseMessage);
    userDataBase.insertUser(user);
  }

  public void unlogin(long userId) {
    userPool.disconnect(userId);
  }

  public void requireUserInfo(Message message) {
    User user = userDataBase.queryUserByUsername(message.getFromId());
    IConnectSession connectSession = userPool.getConnectSession(message.getFromId());
    Message responseMessage = MessageCacheFactory.obtainResponseMessage(message);
    if (connectSession == null) {
      return;
    }

    if (user == null) {
      responseMessage.setType(SystemMessageType.RESPONSE_USER_INFO_FAILED);
      connectSession.writeMessage(responseMessage);
      return;
    }
    responseMessage.setType(SystemMessageType.RESPONSE_USER_INFO);
    responseMessage.setProcessStrategy(MessageProcessStrategy.BODY_ENCRYPTION_MASK);
    MessagePayloadUtils.setString(responseMessage, GsonUtils.GSON.toJson(user));
    connectSession.writeMessage(responseMessage);
  }
}
